package com.ruicar.afs.cloud.basic.sync;

import com.ruicar.afs.cloud.basic.common.entity.*;
import com.ruicar.afs.cloud.basic.common.service.*;
import com.ruicar.afs.cloud.basic.proceeds.mq.dto.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 贷前数据同步-客户类信息
 */
@AllArgsConstructor
@Component
@Slf4j
public class ApplyLoanDataAssetSubHandler {

	private final BasicCarDetailsService basicCarDetailsService;
	private final BasicAffiliatedUnitService basicAffiliatedUnitService;
	private final BasicCarInvoiceService basicCarInvoiceService;
	private final BasicGpsDeviceService basicGpsDeviceService;
	private final BasicInsuranceInfoService basicInsuranceInfoService;
	private final BasicGpsApplyService basicGpsApplyService;

	/*
	 * 合同激活资产类信息存储
	 */
	public void saveOrUpdateAssetInfo(ApplyLoanDataMessage entity) {

		//资产之车辆信息存储
		List<ContractCarDetailsDTO> carDetailList = entity.getCarDetail();

		if (!ObjectUtils.isEmpty(carDetailList)) {
			List<BasicCarDetails> basicCarList = (List<BasicCarDetails>) ApplyLoanDataProcessor.converBean(carDetailList,
					BasicCarDetails.class);
			basicCarDetailsService.saveBatch(basicCarList);
		}
		//资产挂靠信息存储
		List<ContractAffiliatedUnitDTO> affiliatedUnit = entity.getAffiliatedUnit();

		if (!ObjectUtils.isEmpty(affiliatedUnit)) {
			List<BasicAffiliatedUnit> affiliatedUnitList = (List<BasicAffiliatedUnit>) ApplyLoanDataProcessor
					.converBean(affiliatedUnit, BasicAffiliatedUnit.class);
			basicAffiliatedUnitService.saveBatch(affiliatedUnitList);
		}

		//资产发票信息存储
		List<ContractCarInvoiceDTO> carInvoice = entity.getCarInvoice();

		if (!ObjectUtils.isEmpty(carInvoice)) {
			List<BasicCarInvoice> carInvoiceList = (List<BasicCarInvoice>) ApplyLoanDataProcessor.converBean(carInvoice,
					BasicCarInvoice.class);
			basicCarInvoiceService.saveBatch(carInvoiceList);
		}
		//资产GPS设备信息存储
		List<ContractGpsDeviceDTO> gpsDevice = entity.getGpsDevice();

		if (!ObjectUtils.isEmpty(gpsDevice)) {
			List<BasicGpsDevice> gpsDeviceList = (List<BasicGpsDevice>) ApplyLoanDataProcessor.converBean(gpsDevice,
					BasicGpsDevice.class);
			basicGpsDeviceService.saveBatch(gpsDeviceList);
		}
		//资产GPS派单信息存储
		List<ContractGpsApplyDTO> gpsApplyDto = entity.getGpsApply();
		if (!ObjectUtils.isEmpty(gpsApplyDto)) {
			List<BasicGpsApply> gpsApply = (List<BasicGpsApply>) ApplyLoanDataProcessor.converBean(gpsApplyDto,
					BasicGpsApply.class);
			basicGpsApplyService.saveBatch(gpsApply);
		}
		//资产保险信息存储
		List<ContractInsuranceInfoDTO> insuranceInfo = entity.getInsuranceInfo();

		if (!ObjectUtils.isEmpty(insuranceInfo)) {
			List<BasicInsuranceInfo> insuranceInfoList = (List<BasicInsuranceInfo>) ApplyLoanDataProcessor
					.converBean(insuranceInfo, BasicInsuranceInfo.class);
			basicInsuranceInfoService.saveBatch(insuranceInfoList);
		}
	}
}
